package com.ruoyi.project.modules.virtualUsageGroupDetail.service.impl;

import java.util.List;

import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.virtualUsageGroupDetail.mapper.VirtualUsageGroupDetailMapper;
import com.ruoyi.project.modules.virtualUsageGroupDetail.domain.VirtualUsageGroupDetail;
import com.ruoyi.project.modules.virtualUsageGroupDetail.service.IVirtualUsageGroupDetailService;
import com.ruoyi.common.core.text.Convert;

/**
 * 虚拟用况组明细Service业务层处理
 *
 * @author smallrain
 * @date 2022-04-19
 */
@Service
public class VirtualUsageGroupDetailServiceImpl implements IVirtualUsageGroupDetailService {
    @Autowired
    private VirtualUsageGroupDetailMapper virtualUsageGroupDetailMapper;

    /**
     * 查询虚拟用况组明细
     *
     * @param id 虚拟用况组明细主键
     * @return 虚拟用况组明细
     */
    @Override
    public VirtualUsageGroupDetail selectVirtualUsageGroupDetailById(Long id) {
        return virtualUsageGroupDetailMapper.selectVirtualUsageGroupDetailById(id);
    }

    /**
     * 查询虚拟用况组明细列表
     *
     * @param virtualUsageGroupDetail 虚拟用况组明细
     * @return 虚拟用况组明细
     */
    @Override
    public List<VirtualUsageGroupDetail> selectVirtualUsageGroupDetailList(VirtualUsageGroupDetail virtualUsageGroupDetail) {
        return virtualUsageGroupDetailMapper.selectVirtualUsageGroupDetailList(virtualUsageGroupDetail);
    }

    /**
     * 新增虚拟用况组明细
     *
     * @param virtualUsageGroupDetail 虚拟用况组明细
     * @return 结果
     */
    @Override
    public int insertVirtualUsageGroupDetail(VirtualUsageGroupDetail virtualUsageGroupDetail) {
        return virtualUsageGroupDetailMapper.insertVirtualUsageGroupDetail(virtualUsageGroupDetail);
    }

    /**
     * 修改虚拟用况组明细
     *
     * @param virtualUsageGroupDetail 虚拟用况组明细
     * @return 结果
     */
    @Override
    public int updateVirtualUsageGroupDetail(VirtualUsageGroupDetail virtualUsageGroupDetail) {
        return virtualUsageGroupDetailMapper.updateVirtualUsageGroupDetail(virtualUsageGroupDetail);
    }

    /**
     * 批量删除虚拟用况组明细
     *
     * @param ids 需要删除的虚拟用况组明细主键
     * @return 结果
     */
    @Override
    public int deleteVirtualUsageGroupDetailByIds(String ids) {
        return virtualUsageGroupDetailMapper.deleteVirtualUsageGroupDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除虚拟用况组明细信息
     *
     * @param id 虚拟用况组明细主键
     * @return 结果
     */
    @Override
    public int deleteVirtualUsageGroupDetailById(Long id) {
        return virtualUsageGroupDetailMapper.deleteVirtualUsageGroupDetailById(id);
    }

    @Override
    public int saveVirtualUsage(VirtualUsageGroupDetail virtualUsageGroupDetail) {
        if (virtualUsageGroupDetail.getVirtualUsageIds() != null) {
            this.virtualUsageGroupDetailMapper.deleteVirtualUsageGroupDetailByGroupId(virtualUsageGroupDetail.getGroupId());
            int index = 1;
            for (Long virtualUsageId : virtualUsageGroupDetail.getVirtualUsageIds()) {
                virtualUsageGroupDetail.setVirtualUsageId(virtualUsageId);
                virtualUsageGroupDetail.setId(null);
                virtualUsageGroupDetail.setOrderNum(index ++);
                this.virtualUsageGroupDetailMapper.insertVirtualUsageGroupDetail(virtualUsageGroupDetail);
            }
        }
        return 1;
    }

    @Override
    public List<VirtualUsage> virtualUsageList(Long id) {
        return  this.virtualUsageGroupDetailMapper.virtualUsageList(id);
    }
}
